#include "stdafx.h"
#include "./tmMainWindow.h"
#include "./tmTimer.h"
#include "./tmGridMenu.h"
using namespace GLES;

tmMainWindow::tmMainWindow():
_screen(NULL),
_painter(NULL),
_canvas(NULL)
{

}

tmMainWindow::~tmMainWindow()
{
	SAFE_DELETE(_canvas);
	SAFE_DELETE(_painter);
	SAFE_DELETE(_screen);
}

GLES::Bool tmMainWindow::createFrom( HWND hwnd,RectI& rect )
{
	_rect = rect;
	E_Return(_canvas = new Canvas());
	E_Return(_canvas->createWinSurface(hwnd));
	E_Return(_painter = new Painter());
	E_Return(_painter->attachCanvas(_canvas));

	E_Return(_headerBar.createFrom(hwnd,RectI(0,0,800,60)));
	E_Return(_musicBar.createFrom(hwnd,RectI(0,410,800,480)));

	E_Return(_screen = new Canvas());	
	E_Return(_screen->createSurface(rect.getSize()));
	_painter->setScreenSize(rect.getSize());

	_bg.createFromPVR("bg.pvr");
	_menu.initialise(rect);
	return true;
}

GLES::Void tmMainWindow::draw( Painter* painter /*= NULL*/ )
{
	Painter* ptr = painter ? painter : _painter;

	if(!ptr->attachCanvas(_canvas))return;
	if(!ptr->begin())return;
	ptr->drawPict(_rect,&_bg);
	_menu.draw(ptr);
	_headerBar.draw(ptr);
	_musicBar.draw(ptr);
	ptr->end();
	
	ptr->present();
}

GLES::Void tmMainWindow::onTimer( tmTimer* timer )
{
	_menu.onTimer(timer);
}

GLES::Void tmMainWindow::onInput( int x,int y,InputType type,GLES::RectI* parent )
{
	RectI o;
	Tool::translationRect(&o,&_rect,parent);
	_menu.onInput(x,y,type);
	_musicBar.onInput(x,y,type);
}
